<?php

use Johnm\Probackend\Asset\Asset;

// Register all css file
foreach (array_reverse(config('probackend::config.assets.css', array())) as $cssFile) {
    Asset::addFirst('/packages/johnm/probackend/' . $cssFile);
}

// Register all js file
foreach (config('probackend::config.assets.js', array()) as $jsFile) {
    Asset::add('/packages/johnm/probackend/' . $jsFile, "core");
}
define("PACKAGE_RESOURCE_PATH", "/packages/johnm/probackend/");

$prefix = config('probackend::config.sites.prefix');
$clang = config('application.language', 'en');
$flags = config('probackend::config.sites.flags');
$languages = config('probackend::config.sites.languages');
$logo = config('probackend::config.sites.logo');
$logoText = config_translate('probackend::config.sites.logo-text');
$load_error_message = translate('probackend::login.message.send_request_login_error');
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{{translate("probackend::login.page.title")}}</title>
        <?php Asset::css() ?>
    </head>
    <body class="full-width page-condensed">
        <!-- Navbar -->
        <div class="navbar navbar-inverse" role="navigation">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">
                    <img src="{{$logo}}" alt="{{$logoText}}">
                </a>                        
            </div>
            <ul class="nav navbar-nav navbar-right" id="navbar-icons">
                <li class="language dropdown"><a class="dropdown-toggle" data-toggle="dropdown">
                        <img src="{{$flags[$clang]}}"><span>{{$languages[$clang]}}</span><i class="caret"></i></a>
                    <ul class="dropdown-menu dropdown-menu-right icons-right" style="display: none;">
                        @foreach($languages as $lang => $title)
                        <?php if ($lang == $clang) continue; ?>
                        <li><a href="{{URL::route($prefix . '.language',["lang"=>$lang])}}"><img src="{{$flags[$lang]}}"/>{{$title}}</a></li>
                        @endforeach
                    </ul>
                </li>               
            </ul>
        </div>
        <div class="text-center text-center modal-front-drop modal-backdrop in" style="display: none"><i style="font-size: 54px;top: 50%;position: absolute;margin-top: -23px" class="icon-spinner3 spin block-inner"></i></div>
        <!-- /navbar -->
        <!-- Login wrapper -->
        <div class="login-wrapper">
            <form id="login-form" action="{{URL::action("Johnm\Probackend\Controllers\ProBackendController@authen")}}" method="POST" role="form">
                <div class="popup-header">
                    <span class="text-semibold">{{Lang::get("probackend::login.interface.title")}}</span>
                </div>
                <div class="well">
                    <div class="form-group has-feedback">
                        <label>{{Lang::get("probackend::login.interface.email")}}</label>
                        <input name="email" required="true" maxlength="255" type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" class="form-control" placeholder="{{Lang::get("probackend::login.interface.email")}}">
                        <i class="icon-users form-control-feedback"></i></div>
                    <div class="form-group has-feedback">
                        <label>{{Lang::get("probackend::login.interface.password")}}</label>
                        <input name="password"  required="true" pattern=".{6,255}" type="password" class="form-control" placeholder="{{Lang::get("probackend::login.interface.password")}}">
                        <i class="icon-lock form-control-feedback"></i></div>
                    <div class="row form-actions">
                        <div class="col-xs-6">
                            <div class="checkbox checkbox-success">
                                <label>
                                    <input type="checkbox" class="styled">
                                    {{Lang::get("probackend::login.interface.remember")}}</label>
                            </div>
                        </div>
                        <div class="col-xs-6">
                            <button type="submit" class="btn btn-warning pull-right"><i class="icon-menu2"></i>{{Lang::get("probackend::login.interface.submit")}}</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <!-- /login wrapper -->
        <!-- Footer -->
        <div class="footer clearfix">
            <div class="pull-left">{{config_translate("probackend::sites.footer")}}</div>
        </div>
        <!-- /footer -->
        <?php Asset::js("core") ?>
        <script>
            function errorMsg(e) {
                var r = {400: "Server understood the request but request content was invalid.", 401: "Unauthorised access.", 403: "Forbidden resouce can't be accessed", 500: "Internal Server Error.", 503: "Service Unavailable"};
                return r[e]
            }
            $('.dropdown, .btn-group').on('show.bs.dropdown', function() {
                $(this).find('.dropdown-menu').first().stop(true, true).fadeIn(100);
            });
            $('.dropdown, .btn-group').on('hide.bs.dropdown', function() {
                $(this).find('.dropdown-menu').first().stop(true, true).fadeOut(100);
            });
            $(function() {
                $(".styled").uniform({radioClass: "choice", selectAutoWidth: !1})
            }), $('input[name="email"]').focus(function() {
                $(this).parent().removeClass("has-error"), $(this).parent().find("span.label.label-danger").remove()
            }), $('input[name="password"]').focus(function() {
                $(this).parent().removeClass("has-error"), $(this).parent().find("span.label.label-danger").remove()
            }), $("#login-form").ajaxForm({beforeSubmit: function() {
                    $(".modal-backdrop").show()
                }, error: function(e) {
                    $(".modal-backdrop").hide(), $.jGrowl("{{$load_error_message}}</BR>" + e.status + " - " + errorMsg(e.status), {theme: "growl-error"})
                }, success: function(e) {
                    $(".modal-backdrop").hide();
                    var r = JSON.parse(e);
                    1 === r.success ? window.location.href = r.redirect : "undefined" != typeof r.errors.email ? ($('input[name="email"]').parent().addClass("has-error"), $('input[name="email"]').parent().append('<span class="label label-block label-danger">' + r.errors.email[0] + "</span>")) : "undefined" != typeof r.errors.password && ($('input[name="password"]').parent().addClass("has-error"), $('input[name="password"]').parent().append('<span class="label label-block label-danger">' + r.errors.password[0] + "</span>"))
                }});
        </script>
    </body>
</html>